<?php
namespace app\open\controller;
use think\Db;

class DataOrder extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $page = input('page');
            $limit = input('limit');
            $date = explodeTime(input('date'));
            $myUserId = input('my_user_id');
            $roleId = input('role_id');
            $myOrderId = input('my_order_id');
            $cpOrderId = input('cp_order_id');
            $gameId = input('game_id');
            $gameResult = input('game_result');
            $payResult = input('pay_result');
            $debug = input('debug');

            $gameIdStr = session('admin.game_id');
            $where = "game_id in ({$gameIdStr}) && order_time between {$date['start_time']} AND {$date['end_time']}";

            /*点西游特殊处理*/
            if(session('admin.id')==43){
                $where .= " && order_time<1525104000";
            }

            !empty($myUserId) && $where .= " && (my_user_id='{$myUserId}' OR my_user_name like '%{$myUserId}%')";
            !empty($roleId) && $where .= " && (role_id='{$roleId}' OR role_name like '%{$roleId}%')";
            !empty($myOrderId) && $where .= " && my_order_id='{$myOrderId}'";
            !empty($cpOrderId) && $where .= " && cp_order_id='{$cpOrderId}'";
            !empty($gameId) && $where .= " && game_id='{$gameId}'";
            if(!empty($gameResult)){
                if($gameResult == 1){
                    $where .= " && game_result='{$gameResult}'";
                }else{
                    $where .= " && game_result='0'";
                }
            }
            if(!empty($payResult)){
                if($payResult == 1){
                    $where .= " && pay_result='{$payResult}'";
                }else{
                    $where .= " && pay_result='0'";
                }
            }
            if(!empty($debug)){
                if($debug == 1){
                    $where .= " && debug='{$debug}'";
                }else{
                    $where .= " && debug='0'";
                }
            }

            $start = ($page-1)*$limit;
            $length = $limit;

            $y1 = date('Y', $date['start_time']);
            $y2 = date('Y', $date['end_time']);

            if($y1 == $y2){ //同一年
                $count = Db::name('PayOrderY_'.$y1)->where($where)->count();

                $select = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->limit($start, $length)
                    ->order('order_time desc')
                    ->select();

                $sum = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->sum('total_price');
            }else{ //跨年
                $field = "order_time, game_id, my_user_id, my_user_name, role_id, role_name, server_id, server_name, my_order_id, pay_order_id, cp_order_id, total_price, product_name, pay_type, pay_time, pay_result, game_result, ratio, coin, debug, rebate_time";
                $subQuery = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($where)
                    ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$where})", true)
                    ->buildSql();

                $count = Db::table($subQuery.' y')->count();

                $select = Db::table($subQuery.' y')
                    ->limit($start, $length)
                    ->order('order_time desc')
                    ->select();

                $sum = Db::table($subQuery.' y')
                    ->sum('total_price');
            }

            $gameIdName = model('manage/Game')->gameIdName();

            $data = [];
            foreach ($select as $k=>$v){
                $data[] = [
                    'order_time' => date('Y-m-d H:i:s', $v['order_time']),
                    'game_id' => $v['game_id'],
                    'game_name' => $gameIdName[ $v['game_id'] ],
                    'my_user_id' => $v['my_user_id'],
                    'my_user_name' => $v['my_user_name'],
                    'role_id' => $v['role_id'],
                    'role_name' => $v['role_name'],
                    'server_id' => $v['server_id'],
                    'server_name' => $v['server_name'],
                    'cp_order_id' => $v['cp_order_id'],
                    'my_order_id' => $v['my_order_id'],
                    'pay_order_id' => $v['pay_order_id'],
                    'total_price' => '¥ ' . $v['total_price']/100,
                    'product_name' => $v['product_name'],
                    'pay_type' => substr($v['pay_type'], 0, 5)=='wxpay' ? '微信' : '支付宝',
                    'pay_time' => $v['pay_time'] ? date('Y-m-d H:i:s', $v['pay_time']) : '',
                    'pay_result' => $v['pay_result'] ? '到账' : '',
                    'game_result' => $v['game_result'] ? '到账' : '',
                    'ratio' => '1' . '：' . $v['ratio'],
                    'coin' => $v['coin'],
                    'debug' => $v['debug'] ? '测试' : '',
                    'rebate_time' => $v['rebate_time'] ? date('Y-m-d H:i:s', $v['rebate_time']) : '未返利',
                ];
            }

            $this->layTable(0, '支付订单', $data, $count, round($sum/100, 2));
        }else{
            $date = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            $gameData = model('Base')->gameNotice(session('admin.game_id'));
            return view('', ['date'=>$date, 'game_data'=>$gameData]);
        }
    }
}
